package com.climb.moive.dao.impl;

import com.climb.moive.dao.MovieDao;
import com.climb.moive.entity.Movie;
import com.climb.moive.exception.ApplicationException;
import com.climb.moive.framework.jdbc.BeanPropertyRowMapper;
import com.climb.moive.framework.jdbc.JdbcTemplate;
import com.climb.moive.framework.jdbc.RowMapper;
import com.climb.moive.util.JdbcUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class MovieDaoImpl implements MovieDao {

    @Override
    public Integer countMovie() {
        String sql="SELECT COUNT(1) from movie";
        Connection con = null;
        Integer result=0;
        try {
            con = JdbcUtils.getConnection();
            JdbcTemplate jdbcTemplate = new JdbcTemplate(con);
            result=jdbcTemplate.queryForObject(sql,Integer.class);

        } catch (SQLException throwables) {
            new ApplicationException("电影数据访问层查询电影总数出错",throwables);
        } finally {
            JdbcUtils.close(con);
        }
        return result;
    }

    @Override
    public List<Movie> findAllMovie() throws SQLException {
        String sql="SELECT * from movie";
        Connection con = null;
        List<Movie> movies=null;
        try {
            con = JdbcUtils.getConnection();
            JdbcTemplate jdbcTemplate = new JdbcTemplate(con);
            RowMapper<Movie> rowMapper=new BeanPropertyRowMapper<>(Movie.class);
            movies=jdbcTemplate.query(sql,rowMapper);

        } catch (SQLException throwables) {
            new ApplicationException("电影数据访问层查询所有电影",throwables);
        } finally {
            JdbcUtils.close(con);
        }
        return movies;
    }

    @Override
    public Movie findMovieById(Integer id) throws SQLException {
        String sql="SELECT * from movie where id=?";
        Connection con = null;
        Movie movie=null;
        try {
            con = JdbcUtils.getConnection();
            JdbcTemplate jdbcTemplate = new JdbcTemplate(con);
            RowMapper<Movie> MovieRowMapper=new BeanPropertyRowMapper<>(Movie.class);
            movie=jdbcTemplate.queryForObject(sql,MovieRowMapper,id);
        } catch (SQLException throwables) {
            new ApplicationException("电影数据访问层查询所有电影",throwables);
        } finally {
            JdbcUtils.close(con);
        }
        return movie;
    }

    @Override
    public void delete(int id) throws SQLException {
        String sql="DELETE FROM `movie` WHERE `id`=?";
        Connection connection=null;
        try {
            connection=JdbcUtils.getDataSource().getConnection();
            JdbcTemplate jdbcTemplate=new JdbcTemplate(connection);
            jdbcTemplate.update(sql,id);
        }  finally {
            JdbcUtils.close(connection);
        }
    }

    @Override
    public void update(Movie movie) throws SQLException {
        String sql="UPDATE `movie` SET `name`=?, `score`=?,`content`=?,`director`=?, `actor`=?, `language`=?, `production_area`=?, `length`=?, `type`=?, `release_time`=?, `picture`=?  WHERE (`id`=?) ";
        Connection connection=null;
        try {
            connection=JdbcUtils.getDataSource().getConnection();
            JdbcTemplate jdbcTemplate=new JdbcTemplate(connection);
            jdbcTemplate.update(sql, movie.getName(),movie.getScore(),movie.getContent(),movie.getDirector(),movie.getActor(),movie.getLanguage(),movie.getProduction_area(),movie.getLength(),movie.getType(),movie.getRelease_time(),movie.getPicture(),movie.getId());
        }  finally {
            JdbcUtils.close(connection);
        }
    }

    @Override
    public void insert(Movie movie) throws SQLException {
        String sql = "INSERT INTO `movie` (`name`, `score`, `content`, `director`, `actor`, `language`, `production_area`, `length`, `type`, `release_time`, `picture`)  values (?,?,?,?,?,?,?,?,?,?,?)";
        Connection con = null;
        try {
            con = JdbcUtils.getDataSource().getConnection();
            JdbcTemplate jdbcTemplate = new JdbcTemplate(con);
            jdbcTemplate.update(sql,movie.getName(),movie.getScore(),movie.getContent(),movie.getDirector(),movie.getActor(),movie.getLanguage(),movie.getProduction_area(),movie.getLength(),movie.getType(),movie.getRelease_time(),movie.getPicture());
        } finally {
            JdbcUtils.close(con);
        }
    }

    @Override
    public List<Movie> findMoveByDirector(String name) throws SQLException {
        String sql="select * from movie where director like ?";
        Connection connection=null;
        List<Movie> movies=null;
        try {
            connection=JdbcUtils.getConnection();
            JdbcTemplate jdbcTemplate=new JdbcTemplate(connection);
            RowMapper<Movie> movieRowMapper=new BeanPropertyRowMapper<>(Movie.class);
            movies=jdbcTemplate.query(sql,movieRowMapper,"%"+name+"%");
        } finally {
            JdbcUtils.close(connection);
        }
        return movies;
    }

    @Override
    public List<Movie> findMoveByMovieName(String name) throws SQLException {
        String sql="select * from movie where name like ?";
        Connection connection=null;
        List<Movie> movies=null;
        try {
            connection=JdbcUtils.getConnection();
            JdbcTemplate jdbcTemplate=new JdbcTemplate(connection);
            RowMapper<Movie> movieRowMapper=new BeanPropertyRowMapper<>(Movie.class);
            movies=jdbcTemplate.query(sql,movieRowMapper,"%"+name+"%");
        } finally {
            JdbcUtils.close(connection);
        }
        return movies;
    }

    @Override
    public List<Movie> findMoveByActor(String name) throws SQLException {
        String sql="select * from movie where actor like ?";
        Connection connection=null;
        List<Movie> movies=null;
        try {
            connection=JdbcUtils.getConnection();
            JdbcTemplate jdbcTemplate=new JdbcTemplate(connection);
            RowMapper<Movie> movieRowMapper=new BeanPropertyRowMapper<>(Movie.class);
            movies=jdbcTemplate.query(sql,movieRowMapper,"%"+name+"%");
        } finally {
            JdbcUtils.close(connection);
        }
        return movies;
    }
    /**查询近期热映电影*/
    @Override
    public List<Movie> findRecentlyReleaseMovies() throws SQLException {
        String sql="select * from movie order by release_time DESC limit 0,10;";
        Connection connection=null;
        List<Movie> movies=null;
        try {
            connection=JdbcUtils.getConnection();
            JdbcTemplate jdbcTemplate=new JdbcTemplate(connection);
            RowMapper<Movie> movieRowMapper=new BeanPropertyRowMapper<>(Movie.class);
            movies=jdbcTemplate.query(sql,movieRowMapper);
        } finally {
            JdbcUtils.close(connection);
        }
        return movies;
    }


    /**分类查询电影*/
    @Override
    public List<Movie> findMoviesbyType(String type) throws SQLException {
        String sql="select * from movie where type=?  limit 0,10 ";
        Connection connection=null;
        List<Movie> movies=null;
        try {
            connection=JdbcUtils.getConnection();
            JdbcTemplate jdbcTemplate=new JdbcTemplate(connection);
            RowMapper<Movie> movieRowMapper=new BeanPropertyRowMapper<>(Movie.class);
            movies=jdbcTemplate.query(sql,movieRowMapper,type);
        } finally {
            JdbcUtils.close(connection);
        }
        return movies;
    }

    @Override
    public List<Movie> findMoviesbyLanguage(String language) throws SQLException {
        String sql="select * from movie where language=?  limit 0,10 ";
        Connection connection=null;
        List<Movie> movies=null;
        try {
            connection=JdbcUtils.getConnection();
            JdbcTemplate jdbcTemplate=new JdbcTemplate(connection);
            RowMapper<Movie> movieRowMapper=new BeanPropertyRowMapper<>(Movie.class);
            movies=jdbcTemplate.query(sql,movieRowMapper,language);
        } finally {
            JdbcUtils.close(connection);
        }
        return movies;
    }


}
